package com.jsgt.greekfire.ui.home.menu.powerise

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivityForResult
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.PrivilegeWalletBean
import com.jsgt.greekfire.common.util.RecycleEmptyView
import com.jsgt.greekfire.viewmodel.PrivilegeViewModel
import com.scwang.smartrefresh.header.MaterialHeader
import kotlinx.android.synthetic.main.activity_privilege_wallet.*
import kotlinx.android.synthetic.main.view_title.*

class PrivilegeWalletActivity : BaseActivity(), View.OnClickListener {
    private var privilegeType = 0 //0-云创智  1-云创联
    private val listAdapter by lazy {
        PrivilegeWalletAdapter()
    }
    private val privilegeViewModel: PrivilegeViewModel by lazy {
        ViewModelProvider(this).get(PrivilegeViewModel::class.java)
    }
    private var launcher: ActivityResultLauncher<Intent>? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_privilege_wallet
    }

    override fun main() {
        privilegeType = intent.getIntExtra(PrivilegeActivity.TYPE_STR, 0)
        title_tv.text = if (privilegeType == 0) {
            getString(R.string.powerise_wallet)
        } else {
            getString(R.string.cl_wallet)
        }
        right_tv.visibility = View.VISIBLE
        right_tv.text = if (privilegeType == 0) {
            getString(R.string.powerise_income)
        } else {
            getString(R.string.cl_income)
        }
        right_tv.setOnClickListener(this)

        refresh.setEnableLoadMore(false)
        refresh.setRefreshHeader(MaterialHeader(this).setColorSchemeResources(R.color.black))
        refresh.setOnRefreshListener {
            privilegeViewModel.getPrivilegeWallet(privilegeType)
        }

        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = listAdapter
        launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == Activity.RESULT_OK) {
                refresh.autoRefresh()
            }
        }
        listAdapter.setOnItemChildClickListener(object : OnItemChildClickListener {
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int
            ) {
                launcher?.launch(
                    Intent(
                        this@PrivilegeWalletActivity,
                        PrivilegeTransferActivity::class.java
                    ).apply {
                        putExtra(
                            PrivilegeTransferActivity.INTENT_DATA,
                            listAdapter.getItem(position)
                        )
                        putExtra(PrivilegeActivity.TYPE_STR, privilegeType)
                    })
            }
        })
        refresh.autoRefresh()

        privilegeViewModel.walletListData.observe(this, Observer {
            if (it.isEmpty()) {
                RecycleEmptyView.showEmptyView(
                    this,
                    listAdapter,
                    getString(R.string.privilege_wallet_non),
                    R.mipmap.icon_empty_no_data
                )
            } else {
                listAdapter.setNewInstance(it as MutableList<PrivilegeWalletBean>)
            }
            refresh.finishRefresh()
        })
        privilegeViewModel.errorLiveData.observe(this, Observer {
            showToast(it.toString())
            refresh.finishRefresh()
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.right_tv -> {
                showToast(getString(R.string.not_yet_open))
            }
        }
    }
}